package com.estronger.shopmanage.manager;

import com.estronger.shopmanage.bean.B16PlanMeasure;
import com.estronger.shopmanage.bean.B34BuildDetailGoods;
import com.estronger.shopmanage.bean.batch.BatchPgGoodsItem;
import com.estronger.shopmanage.bean.conditionitem;
import com.estronger.shopmanage.bean.measureitem;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Soonfor on 2017/5/22.
 */

public class DataManager {
    public static Map<String, Map<String, List<B16PlanMeasure.MeasureGoodsCondition>>> selectedCondMap_M
            = new HashMap<String, Map<String, List<B16PlanMeasure.MeasureGoodsCondition>>>();
    ;
    public static Map<String, Map<String, List<B34BuildDetailGoods.BuildCondition>>> selectedCondMap_I
            = new HashMap<String, Map<String, List<B34BuildDetailGoods.BuildCondition>>>();
    //测量派工
    public static Map<String, List<B16PlanMeasure.MeasureGoodsCondition>> condMap
            = new HashMap<String, List<B16PlanMeasure.MeasureGoodsCondition>>();
    public static Map<String, measureitem> msiteMap
            = new HashMap<>();


    //msiteMap转json（测量）
    public static String getJsonByMap(Map<String, measureitem> map) {
        List lt = new ArrayList();
        JSONArray jr = new JSONArray();
        try {
            for (String i : map.keySet()) {
                JSONObject o = new JSONObject();
                measureitem p = map.get(i);
                o.put("measureitemid", p.getMeasureitemid());
                o.put("conditionitem", conditionitemToJSON(p.getConList()));
                lt.add(o);
            }
            jr = new JSONArray(lt);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return getRidOfSprit(jr.toString());
    }

    //为没有选择条件的选项设值
    public static void setNoSelectItem() {
        ArrayList<String> conList = setMapsetToList1();
        ArrayList<String> meList = setMapsetToListCom(msiteMap);
        if (meList.size() < conList.size()) {
            for (int i = 0; i < conList.size(); i++) {
                for (int j = 0; j < meList.size(); j++) {
                    if (!conList.get(i).equals(meList.get(j))) {
                        msiteMap.put(conList.get(i), new measureitem(conList.get(i), null));
                    }
                }
            }
        }
    }

    //安装派工
    public static Map<String, List<B34BuildDetailGoods.BuildCondition>> isCondMap
            = new HashMap<String, List<B34BuildDetailGoods.BuildCondition>>();
    public static Map<String, measureitem> isItemMap
            = new HashMap<>();

    //msiteMap转json（安装）
    public static String getJsonByMap2(Map<String, measureitem> map) {
        List lt = new ArrayList();
        JSONArray jr = new JSONArray();
        try {
            for (String i : map.keySet()) {
                JSONObject o = new JSONObject();
                measureitem p = map.get(i);
                o.put("dlvitemid", p.getMeasureitemid());
                o.put("conditionitem", conditionitemToJSON(p.getConList()));
                lt.add(o);
            }
            jr = new JSONArray(lt);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return getRidOfSprit(jr.toString());
    }

    /*
     *对象conditionitem集合转换成json
     *@param list
     * @return
     */
    public static JSONArray conditionitemToJSON(ArrayList<conditionitem> zlist) {

        if (null == zlist || zlist.size() == 0) {
            return null;
        }
        JSONArray json = new JSONArray();
        for (int i = 0; i < zlist.size(); i++) {
            JSONObject jz = new JSONObject();
            conditionitem za = zlist.get(i);
            try {
                jz.put("itemid", za.getItemid());
                jz.put("ifcheck", za.getIfcheck());
                json.put(jz);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return json;
    }

    /**
     * 批量派工测量任务产品明细转为json
     *
     * @return
     */
    public static JSONArray batchMesureGoodsToJson(ArrayList<BatchPgGoodsItem> goosList) {
        JSONArray jr = new JSONArray();
        try {
            for (BatchPgGoodsItem goodsItem : goosList) {
                JSONObject o = new JSONObject();
                o.put("measureitemid", goodsItem.getMeasureitemid());
                o.put("conditionitem", batchConditionitemToJSON(goodsItem.getConditionitem()));
                jr.put(o);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jr;
    }

    /**
     * 批量派工安装任务产品明细转为json
     *
     * @return
     */
    public static JSONArray batchInstallGoodsToJson(ArrayList<BatchPgGoodsItem> goosList) {
        JSONArray jr = new JSONArray();
        try {
            for (BatchPgGoodsItem goodsItem : goosList) {
                JSONObject o = new JSONObject();
                o.put("dlvitemid", goodsItem.getDlvitemid());
                o.put("conditionitem", batchConditionitemToJSON(goodsItem.getConditionitem()));
                jr.put(o);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jr;
    }

    /*
     *对象conditionitem集合转换成json
     *@param list
     * @return
     */
    public static JSONArray batchConditionitemToJSON(ArrayList<BatchPgGoodsItem.GoodsCondition> zlist) {
        JSONArray ja_array = new JSONArray();
        if (null == zlist || zlist.size() == 0) {
            return ja_array;
        }
        for (int i = 0; i < zlist.size(); i++) {
            JSONObject jz = new JSONObject();
            BatchPgGoodsItem.GoodsCondition za = zlist.get(i);
            try {
                jz.put("itemid", za.getItemid());
                jz.put("ifcheck", za.getIfcheck());
                ja_array.put(jz);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ja_array;
    }

    //为没有选择条件的选项设值
    public static void setNoSelectItem_Install() {
        ArrayList<String> conList = setMapsetToList2();
        ArrayList<String> installList = setMapsetToListCom(isItemMap);
        if (installList.size() < conList.size()) {
            for (int i = 0; i < conList.size(); i++) {
                for (int j = 0; j < installList.size(); j++) {
                    if (!conList.get(i).equals(installList.get(j))) {
                        isItemMap.put(conList.get(i), new measureitem(conList.get(i), null));
                    }
                }
            }
        }

    }

    /*
     去掉反斜杠
     */
    public static String getRidOfSprit(String jsonStr) {
        if (jsonStr.contains("\\")) {
            jsonStr = jsonStr.replace("\\", "");
        }
        return jsonStr;
    }

    public static ArrayList<String> setMapsetToList1() {
        ArrayList<String> sList = new ArrayList<>();
        if (condMap != null && condMap.size() > 0) {
            for (String s1 : condMap.keySet()) {
                sList.add(s1);
            }
        }
        return sList;
    }

    public static ArrayList<String> setMapsetToList2() {
        ArrayList<String> _sList = new ArrayList<>();
        if (isCondMap != null && isCondMap.size() > 0) {
            for (String s2 : isCondMap.keySet()) {
                _sList.add(s2);
            }
        }
        return _sList;
    }

    public static ArrayList<String> setMapsetToListCom(Map<String, measureitem> mMap) {
        ArrayList<String> comList = new ArrayList<>();
        if (mMap != null && mMap.size() > 0) {
            for (String comS : mMap.keySet()) {
                comList.add(comS);
            }
        }
        return comList;
    }
}
